package d20chat;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GeneralViewer.java
 *
 * Created on Jul 30, 2009, 3:29:00 PM
 */



import java.util.ArrayList;
import javax.swing.JOptionPane;

import d20chat.D20Chat;


/**
 *
 * @author Arul
 */
public class GeneralViewer extends javax.swing.JFrame {

    private ArrayList <CharSheetViewer35> charSheets35;
    private ArrayList <CharSheetViewer20> charSheets20;
    D20Chat main;

    /** Creates new form GeneralViewer */
    public GeneralViewer(D20Chat d) {
        initComponents();
        main = d;
        charSheets35=new ArrayList<CharSheetViewer35>();
        charSheets20=new ArrayList<CharSheetViewer20>();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        menuBar = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        newSheet = new javax.swing.JMenuItem();
        openSheet = new javax.swing.JMenuItem();
        saveSheet = new javax.swing.JMenuItem();
        saveSheetAs = new javax.swing.JMenuItem();
        closeSheet = new javax.swing.JMenuItem();
        exitSheet = new javax.swing.JMenuItem();
        edit = new javax.swing.JMenu();
        updateTitles = new javax.swing.JMenuItem();
        view = new javax.swing.JMenu();
        viewSkillPoints = new javax.swing.JMenuItem();
        help = new javax.swing.JMenu();
        viewHelp = new javax.swing.JMenuItem();

        file.setMnemonic('f');
        file.setText("File");

        newSheet.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newSheet.setMnemonic('n');
        newSheet.setText("New");
        newSheet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newSheetActionPerformed(evt);
            }
        });
        file.add(newSheet);

        openSheet.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openSheet.setMnemonic('o');
        openSheet.setText("Open");
        openSheet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openSheetActionPerformed(evt);
            }
        });
        file.add(openSheet);

        saveSheet.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveSheet.setMnemonic('s');
        saveSheet.setText("Save");
        saveSheet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveSheetActionPerformed(evt);
            }
        });
        file.add(saveSheet);

        saveSheetAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
        saveSheetAs.setText("Save As...");
        saveSheetAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveSheetAsActionPerformed(evt);
            }
        });
        file.add(saveSheetAs);

        closeSheet.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        closeSheet.setText("Close Tab");
        closeSheet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeSheetActionPerformed(evt);
            }
        });
        file.add(closeSheet);

        exitSheet.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        exitSheet.setText("Exit");
        exitSheet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitSheetActionPerformed(evt);
            }
        });
        file.add(exitSheet);

        menuBar.add(file);

        edit.setMnemonic('e');
        edit.setText("Edit");

        updateTitles.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
        updateTitles.setText("Update Titles");
        updateTitles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateTitlesActionPerformed(evt);
            }
        });
        edit.add(updateTitles);

        menuBar.add(edit);

        view.setMnemonic('v');
        view.setText("View");

        viewSkillPoints.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        viewSkillPoints.setText("Skill Points");
        viewSkillPoints.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewSkillPointsActionPerformed(evt);
            }
        });
        view.add(viewSkillPoints);

        menuBar.add(view);

        help.setMnemonic('h');
        help.setText("Help");

        viewHelp.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        viewHelp.setText("View Help");
        viewHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewHelpActionPerformed(evt);
            }
        });
        help.add(viewHelp);

        menuBar.add(help);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**Prompts the user for the desired type of character sheet, and then
     * creates the appropriate GeneralViewer.  It then adds each page to this
     * generalized viewer's JTabbedPane.
     */
    public void newSheet(){
        Object[] options = {"D&D 3.5",
                    "d20 Modern"};
        int n = JOptionPane.showOptionDialog(this,
            "Please choose the type of character sheet:",
            "Character Sheet Type",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,     //do not use a custom Icon
            options,  //the titles of buttons
            options[0]); //default button title

        switch(n){
            case JOptionPane.CLOSED_OPTION:
            case JOptionPane.YES_OPTION:CharSheetViewer35 view35=new CharSheetViewer35(this);
                                        tabbedPane.add(view35.getCharName()+" P1", view35.getPage1());
                                        tabbedPane.add(view35.getCharName()+" P2", view35.getPage2());
                                        charSheets35.add(view35);
                                        charSheets35.add(view35);
                                        charSheets20.add(null);
                                        charSheets20.add(null);
                                        break;
            case JOptionPane.NO_OPTION:CharSheetViewer20 view20=new CharSheetViewer20(this);
                                       tabbedPane.add(view20.getCharName()+" P1", view20.getPage1());
                                       tabbedPane.add(view20.getCharName()+" P2", view20.getPage2());
                                       charSheets20.add(view20);
                                       charSheets20.add(view20);
                                       charSheets35.add(null);
                                       charSheets35.add(null);
                                       break;
            default:System.out.println("No Hacking");
        }

        if(charSheets20.get(0)!=null){
            charSheets20.get(0).setIsPlayerSheet(true);
        }else if(charSheets35.get(0)!=null){
            charSheets35.get(0).setPlayerSheet(true);
        }
    }

    /**Prompts the user for the desired type of character sheet, and then
     * creates the appropriate GeneralViewer, prompting the user for the
     * file to open.  It then adds each page to this generalized viewer's
     * JTabbedPane.
     */
    public void openSheet(){
        Object[] options = {"D&D 3.5",
                    "d20 Modern"};
        int n = JOptionPane.showOptionDialog(this,
            "Please choose the type of character sheet:",
            "Character Sheet Type",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,     //do not use a custom Icon
            options,  //the titles of buttons
            options[0]); //default button title

        switch(n){
            case JOptionPane.CLOSED_OPTION:
            case JOptionPane.YES_OPTION:CharSheetViewer35 view35=new CharSheetViewer35(this);
                                        view35.open();
                                        tabbedPane.add(view35.getCharName()+" P1", view35.getPage1());
                                        tabbedPane.add(view35.getCharName()+" P2", view35.getPage2());
                                        charSheets35.add(view35);
                                        charSheets35.add(view35);
                                        charSheets20.add(null);
                                        charSheets20.add(null);
                                        break;
            case JOptionPane.NO_OPTION:CharSheetViewer20 view20=new CharSheetViewer20(this);
                                       view20.open();
                                       tabbedPane.add(view20.getCharName()+" P1", view20.getPage1());
                                       tabbedPane.add(view20.getCharName()+" P2", view20.getPage2());
                                       charSheets20.add(view20);
                                       charSheets20.add(view20);
                                       charSheets35.add(null);
                                       charSheets35.add(null);
                                       break;
            default:System.out.println("No Hacking");
        }

        if(charSheets20.get(0)!=null){
            charSheets20.get(0).setIsPlayerSheet(true);
        }else if(charSheets35.get(0)!=null){
            charSheets35.get(0).setPlayerSheet(true);
        }
    }
    //TODO: parse and autoselect type when opening a charsheet

    public void saveSheet(){
       int sel=tabbedPane.getSelectedIndex();
       CharSheetViewer35 view35=charSheets35.get(sel);
       CharSheetViewer20 view20=charSheets20.get(sel);
       if(view35!=null)
           view35.save();
       else if(view20!=null)
           view20.save();
    }

    public void saveSheetAs(){
       int sel=tabbedPane.getSelectedIndex();
       CharSheetViewer35 view35=charSheets35.get(sel);
       CharSheetViewer20 view20=charSheets20.get(sel);
       if(view35!=null)
           view35.saveAs();
       else if(view20!=null)
           view20.saveAs();
    }

    public void closeSheet(){
       String title=tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
       String name=title.substring(0,title.length()-1);
       int n=title.charAt(title.length()-1);
       switch(n){
           case '1': case '2':
                           int j=tabbedPane.indexOfTab(name+"1");
                           tabbedPane.removeTabAt(j);
                           int k=tabbedPane.indexOfTab(name+"2");
                           tabbedPane.removeTabAt(k);
                           CharSheetViewer35 view35=charSheets35.remove(j);
                           charSheets35.remove(k);
                           if(view35!=null){
                               view35.dispose();
                               view35=null;
                           }
                           CharSheetViewer20 view20=charSheets20.remove(j);
                           charSheets20.remove(k);
                           if(view20!=null){
                               view20.dispose();
                               view20=null;
                           }
                           break;
           default: tabbedPane.remove(tabbedPane.getSelectedIndex());
       }
    }

    public void displaySheet(String inText){
        if (tabbedPane.getTabCount()>1) {
            int type = Integer.parseInt(inText.substring(0, 2));

            switch (type) {
                case 20:
                    CharSheetViewer20 view20 = new CharSheetViewer20(this, inText);
                    tabbedPane.add(view20.getCharName() + " P1", view20.getPage1());
                    tabbedPane.add(view20.getCharName() + " P2", view20.getPage2());
                    charSheets20.add(view20);
                    charSheets20.add(view20);
                    charSheets35.add(null);
                    charSheets35.add(null);
                    break;
                case 35:
                    CharSheetViewer35 view35 = new CharSheetViewer35(this, inText);
                    tabbedPane.add(view35.getCharName() + " P1", view35.getPage1());
                    tabbedPane.add(view35.getCharName() + " P2", view35.getPage2());
                    charSheets35.add(view35);
                    charSheets35.add(view35);
                    charSheets20.add(null);
                    charSheets20.add(null);
                    break;
                default:
                    System.out.println("No Hacking.");
            }
        }else{
            JOptionPane.showMessageDialog(this, "Error:  you must have your sheet open first.");
        }
    }

    public void setSheetData(String inText, String charName){
        int index=-1;
        for(int j=0;j<tabbedPane.getTabCount();j++){
            if(tabbedPane.getTitleAt(j).indexOf(charName)>-1){
               index=j;
               break;
            }
        }

        if(index<0 || index>tabbedPane.getTabCount()-1)
            System.err.println("ERROR: Invalid name for setSheetData");
        else{
            CharSheetViewer35 view35=charSheets35.get(index);
            CharSheetViewer20 view20=charSheets20.get(index);
            if(view35!=null)
                view35.setData(inText);
            else if(view20!=null)
                view20.setData(inText);
            else
                System.err.println("ERROR: Could not set sheet data.");
        }
    }

    public void setSheetData(String inText, int index){
        if(index<0 || index>tabbedPane.getTabCount()-1)
            System.err.println("ERROR: Invalid tab location for setSheetData");
        else{
            CharSheetViewer35 view35=charSheets35.get(index);
            CharSheetViewer20 view20=charSheets20.get(index);
            if(view35!=null)
                view35.setData(inText);
            else if(view20!=null)
                view20.setData(inText);
            else
                System.err.println("ERROR: Could not set sheet data.");
        }
    }

    public void updateCharData(String newData){
        main.gui.characterSheetEditorPane.setText(newData);
        main.gui.characterNameTextField.setText(newData.substring(5, newData.indexOf('#')));
    }

    private void newSheetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newSheetActionPerformed
        newSheet();
}//GEN-LAST:event_newSheetActionPerformed

    private void openSheetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openSheetActionPerformed
        openSheet();
}//GEN-LAST:event_openSheetActionPerformed

    private void saveSheetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveSheetActionPerformed
        saveSheet();
}//GEN-LAST:event_saveSheetActionPerformed

    private void saveSheetAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveSheetAsActionPerformed
        saveSheetAs();
}//GEN-LAST:event_saveSheetAsActionPerformed

    private void closeSheetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeSheetActionPerformed
        closeSheet();
}//GEN-LAST:event_closeSheetActionPerformed

    private void viewSkillPointsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewSkillPointsActionPerformed
       int sel=tabbedPane.getSelectedIndex();
        if (sel!=-1) {
            CharSheetViewer35 view35 = charSheets35.get(sel);
            CharSheetViewer20 view20 = charSheets20.get(sel);
            int sp = 0;
            if (view35 != null) {
                sp = view35.getSkillPts();

            } else if (view20 != null) {
                sp = view20.getSkillPts();


            }
            String msg = "Total skill points used: " + sp;

            JOptionPane.showMessageDialog(this, msg, "Skill Points", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_viewSkillPointsActionPerformed

    private void updateTitlesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateTitlesActionPerformed
        for(int j=0;j<tabbedPane.getTabCount();j++){
            CharSheetViewer35 view35=charSheets35.get(j);
            CharSheetViewer20 view20=charSheets20.get(j);
            String charName="";
            if(view35!=null)
                charName=view35.getCharName();
            else if(view20!=null)
                charName=view20.getCharName();
            String oldName=tabbedPane.getTitleAt(j);
            tabbedPane.setTitleAt(j, charName+oldName.substring(oldName.length()-3));
        }
    }//GEN-LAST:event_updateTitlesActionPerformed

    private void exitSheetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitSheetActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_exitSheetActionPerformed

    private void viewHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewHelpActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_viewHelpActionPerformed

    

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        GeneralViewer view=new GeneralViewer(new D20Chat());
        view.setVisible(true);

        String test="CharName P1";
        System.out.println(test.substring(test.length()-3));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem closeSheet;
    private javax.swing.JMenu edit;
    private javax.swing.JMenuItem exitSheet;
    private javax.swing.JMenu file;
    private javax.swing.JMenu help;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newSheet;
    private javax.swing.JMenuItem openSheet;
    private javax.swing.JMenuItem saveSheet;
    private javax.swing.JMenuItem saveSheetAs;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JMenuItem updateTitles;
    private javax.swing.JMenu view;
    private javax.swing.JMenuItem viewHelp;
    private javax.swing.JMenuItem viewSkillPoints;
    // End of variables declaration//GEN-END:variables

}
